package com.sx.gongzhong.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.MyLettersAdapter;
import com.sx.gongzhong.model.LetterBean;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.pest.DeletePest;
import com.sx.gongzhong.pest.MyLettersListPest;
import com.sx.gongzhong.utils.AppConstants;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.IVoidView;
import com.sx.gongzhong.view.MyLettersListView;
import com.sx.gongzhong.widget.CustomDialog;
import com.sx.gongzhong.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**我的信访举报
 * Created by admin on 2018/1/3.
 */

public class MyLettersActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, MyLettersListView, IVoidView {

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private List<LetterBean> mlist;
    private MyLettersAdapter adapter;

    private int page = 1, pagesize = 10;
    private int deletePosition = -1;
    private Disposable disposable;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.refresh_list);
        ButterKnife.bind(this);
        setTitleName("我的信访举报");
        initRx();
        initView();
    }
    private void initRx() {
        disposable =  MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent msgEvent) throws Exception {
                        if ("MyLettersActivity".equals(msgEvent.getRequest())) {
                            if (msgEvent.getType()== 1){
                                page = 1;
                                getData();
                            }
                        }
                    }
                });
    }

    private void initView() {
        mlist = new ArrayList<>();
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(MyLettersActivity.this, LinearLayoutManager.VERTICAL, false));
        adapter = new MyLettersAdapter(this, R.layout.item_my_letter, mlist);
        adapter.setEmptyView(View.inflate(this, R.layout.load_error_view, null));
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
            }
        }, recyclerView);
        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                deletePosition = position;
                createDialog();
                return true;
            }
        });
        onRefresh();
    }

    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(false);
        page = 1;
        getData();
    }

    private void getData() {
        new MyLettersListPest(this).getMyLettersListData(this,page,pagesize);
    }

    @Override
    public void getSuccess(List<LetterBean> list,int total) {
        if (page == 1) {
            mlist.clear();
            adapter.setNewData(mlist);
        }
        if (list != null && list.size() > 0) {
            mlist.addAll(list);
        }
        if (page == total) {
            adapter.loadMoreEnd();
        }else {
            adapter.loadMoreComplete();
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(MyLettersActivity.this);
        ToastUtils.showToast(MyLettersActivity.this,message);
    }
    /**
     * 删除
     */
    public void createDialog() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        CustomDialog dialog = builder.setMessage("确定删除吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                LoadDialog.show(MyLettersActivity.this,"删除中...");
                new DeletePest(MyLettersActivity.this).deleteMyAdd("/api/columnManage/lettersAndCallsReport/delete",mlist.get(deletePosition).getId());
            }
        }).create();
        dialog.show();
    }
    @Override
    public void voidView(String result) {
        LoadDialog.dismiss(MyLettersActivity.this);
        ToastUtils.showToast(MyLettersActivity.this,result);
        mlist.remove(deletePosition);
        adapter.notifyDataSetChanged();
    }
    @Override
    protected void onDestroy() {
        if (disposable != null){
            disposable = null;
        }
        super.onDestroy();
    }
}
